﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Gamev1
{
    public class XML
    {
        public void save(List<Sprite> sprites, int levelnumber)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Sprite>));
            CreateDirectory("myLevels");
            TextWriter textWriter = new StreamWriter(GetProjectPath() + @"\x86\Debug\myLevels\level_" + levelnumber + ".xml");
            serializer.Serialize(textWriter, sprites);
            textWriter.Close();
        }

        public List<Sprite> load(int levelnumber)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(List<Sprite>));
            TextReader textReader = new StreamReader(GetProjectPath() + @"\x86\Debug\myLevels\level_" + levelnumber + ".xml");
            List<Sprite> sprites = (List<Sprite>)deserializer.Deserialize(textReader);
            textReader.Close();
            return sprites;
        }

        private String GetProjectPath()
        {
            return Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()));
        }

        private void CreateDirectory(string path)
        {
            if (!Directory.Exists(path))
            {
                DirectoryInfo dir = Directory.CreateDirectory(path);
            }
        }
    }
}
